<?php

/**
 * 资讯控制器
 *
 *
 * @package  app
 * @extends  Controller
 */
namespace web;

class Controller_Admin_Article extends \Controller_FilterController {

    public $template = 'admin/default/template';

    public function before(){
        parent::before();
    }

    /**
    * 资讯列表
    * 
    **/
    public function action_index(){
        $params = array(
            'title' => '资讯管理',
            'menu' => 'member-index'
        );

        $ids = \Model_Category::getChildIds(\Session::get('seller')->article_category);
        array_push($ids, \Session::get('seller')->article_category);

        $items = \Model_Node::query()
            ->where('category_id', 'IN', $ids);

        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/web/admin/article",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn_ajax'
        );

        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();
        if(\Input::is_ajax()){
            $items = array();
            foreach ($params['items'] as $key => $value) {
                $item = $value->to_array();
                $item['created_at'] = date('Y-m-d H:i:s', $value->created_at);
                $item['author'] = $value->author->to_array();
                $item['category'] = $value->category->to_array();
                array_push($items, $item);
            }
            die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $items, 'pagination' => htmlspecialchars_decode($pagination))));
        }

        \View::set_global($params);
        $this->template->content = \View::forge('admin/default/article/index');
    }

    /**
    * 创建或编辑资讯信息
    * 
    * 当资讯ID不为0时的操作为编辑资讯。反之为创建
    * @param $id 资讯ID
    * 
    **/
    public function action_save($id = 0){
        $params = array(
            'title' => $id ? '编辑资讯' : '发布资讯',
            'menu' => 'aticle-index'
        );

        if($id){
            $node = \Model_Node::find($id);
        }

        if(\Input::method() == 'POST'){
            if(! \Security::check_token()){
                die('过期或无效的请求');
            }
            $data = \Input::post();
            if(isset($node) && $node){
                $node->set($data);
            }else{
                $node = \Model_Node::forge($data);
            }
            
            if($node->save()){
                \Session::set_flash('msg', array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)); 
            }else{
                \Session::set_flash('msg', array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)); 
            }
        }

        $cid = \Input::get('cid', false) == false ? \Session::get('seller')->article_category : \Session::get('seller')->car_param;
        $cat = \Model_Category::find($cid);
        $params['cats'] = $cat->children()->get();
        if( ! $params['cats']){
            $params['cats'] = array($cat);
        }

        if(isset($node) && $node){
            $params['item'] = $node;
        }

        \View::set_global($params);
        $this->template->content = \View::forge('admin/default/article/details');
    }

    /**
    * 资讯类目列表
    **/
    public function action_categories(){

    }

    /**
    * 创建或编辑资讯类目
    *
    * @param $id 资讯类目ID
    **/
    public function action_category_save($id = 0){
        
    }
}